<template>
  <el-dialog :visible.sync="visible" title="编辑课程">
    <el-form label-width="120px" :model="form">
      <el-form-item prop="discipline_id" label="课程">
        <el-select v-model="form.discipline_id">
          <template v-for="item in extDisciplines">
            <el-option :value="item.id" :label="item.name" />
          </template>
          <template v-for="item in disciplines">
            <el-option :value="item.id" :label="item.name" />
          </template>
        </el-select>
      </el-form-item>

      <el-form-item prop="class_id" label="班级">
        <el-select v-model="form.class_id">
          <template v-for="item in classes">
            <el-option :value="item.id" :label="item.name" />
          </template>
        </el-select>
      </el-form-item>

      <el-form-item prop="mode" label="授课方式">
        <el-select v-model="form.mode">
          <el-option :value="0" label="行政班授课" />
          <el-option :value="1" label="合班授课" />
          <el-option :value="2" label="选修班授课" />
        </el-select>
      </el-form-item>

      <el-form-item prop="test_type" label="考试方式">
        <el-select v-model="form.test_type">
          <el-option :value="0" label="考试" />
          <el-option :value="1" label="考查" />
        </el-select>
      </el-form-item>

      <el-row>
        <el-col :span="12">
          <el-form-item prop="week_hour" label="周课时">
            <el-input-number v-model="form.week_hour" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item prop="series" label="连排课时">
            <el-input-number v-model="form.series" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item prop="start_week" label="排课开始周">
            <el-input-number v-model="form.start_week" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item prop="start_week" label="排课结束周">
            <el-input-number v-model="form.end_week" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item prop="score_type" label="分制类型">
            <el-select v-model="form.score_type">
              <el-option value="百分制" />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item prop="score" label="学分">
            <el-input-number v-model="form.score" />
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>

    <div slot="footer">
      <el-button @click="visible = false">取 消</el-button>

      <el-button type="primary" @click="onConfirm" :loading="subing">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import _ from "lodash";
import arrange from "@/api/v2/arrange";

export default {
  name: 'VPaikeForm',
  props: {
    value: Object
  },
  computed: {
    visible: {
      get() {
        return _.size(this.value) > 0
      },
      set(v) {
        if (!v) {
          this.$emit('input', {})
        }
      }
    }
  },
  watch: {
    visible(v) {
      if (v) {
        this.form = {...this.value}

        // 获取班级
        this.$api.setting.getClassList()
            .then(result => {
              if (result.data.code === 1) {
                this.classes = result.data.data.rows;
              }
            })
        // 获取课程
        this.$api.setting.subjectIndex({
          limit: 100,
          ext_ids: [
              this.form.discipline_id,
          ]
        }).then((result) => {
          if (result.data.code === 1) {
            this.disciplines = result.data.data.rows;
            this.extDisciplines = result.data.data.exts;
          }
        })
      }
    }
  },
  data() {
    return {
      form: {},
      disciplines: [],
      extDisciplines: [],
      classes: [],
      subing: false
    }
  },
  mounted() {
  },
  methods: {
    onConfirm() {
      this.subing = true
      arrange.edit(this.form)
          .then(() => {
            this.$message.success('更新成功')
            this.visible = false
            this.$emit('success')
          })
          .finally(() => this.subing = false)
    }
  }
}
</script>

<style scoped>

</style>